package com.article.mail;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * Created by Administrator on 2017/5/24.
 */
public class SendMultFileEmail {
    public static void main(String[] args) {
        // 收件人电子邮箱
        String to = "1064322591@qq.com";

        // 发件人电子邮箱
        String from = "xucong0213@126.com";

        // 指定发送邮件的主机为 smtp.126.com
        String host = "smtp.126.com"; // 126 邮件服务器

        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);

        properties.put("mail.smtp.auth", "true");
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xucong0213@126.com", "xxxx"); // 发件人邮件用户名、密码
            }
        });

        try {
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            // Set Subject: 头部头字段
            message.setSubject("This is the Subject Line!");
            // 创建消息部分
            BodyPart messageBodyPart = new MimeBodyPart();

            // 消息
            messageBodyPart.setText("This is message body");

            // 创建多重消息
            Multipart multipart = new MimeMultipart();

            // 设置文本消息部分
            multipart.addBodyPart(messageBodyPart);

            // 附件部分
            messageBodyPart = new MimeBodyPart();

            String[] files = new String[] {
                "C:\\Users\\Administrator\\Desktop\\javamail1_4_5\\javamail-1.4.5\\demo\\README.txt", "C:\\Users\\Administrator\\Desktop\\javamail1_4_5\\javamail-1.4.5\\demo\\README.txt"
            } ;
            addTach(files,multipart);


            //单个附件
            //File file = new File("C:\\Users\\Administrator\\Desktop\\javamail1_4_5\\javamail-1.4.5\\demo\\README.txt");
            //String filename = "file.txt";
            //DataSource source = new FileDataSource(file);
            //messageBodyPart.setDataHandler(new DataHandler(source));
            //messageBodyPart.setFileName(filename);
            //multipart.addBodyPart(messageBodyPart);

            // 发送完整消息
            message.setContent(multipart);

            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


    //添加多个附件
    public static void addTach(String[] fileList, Multipart multipart) throws MessagingException, UnsupportedEncodingException {
        for (int i = 0; i < fileList.length; i++) {
            MimeBodyPart mimeBodyPart = new MimeBodyPart();
            FileDataSource fds = new FileDataSource(fileList[i]);
            mimeBodyPart.setDataHandler(new DataHandler(fds));
            mimeBodyPart.setFileName(MimeUtility.encodeText(fds.getName()+i, "GBK", "B"));
            multipart.addBodyPart(mimeBodyPart);
        }
    }
}
